package com.legolas.provider.api.serviceapi.activemq;

import com.legolas.api.utils.Consts;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.springframework.stereotype.Service;

import javax.jms.*;

@Service
public class QueueProducer {

    //ActiveMQ集群提供3台服务器最佳
    //b、c两台服务器作为生产者服务器，将生产的消息同步到主服务器a上
    private static final String slv_url = "failover:(tcp://127.0.0.1:61617,tcp:127.0.0.1:61618)?randomize=true";

    private static final String url = "tcp://127.0.0.1:61616";
    //private static final String queueName = "provider1-queue-test";

    public void produce(String msg) throws JMSException {

        ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url);

        Connection connection = connectionFactory.createConnection();

        connection.start();

        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        //创建一个目标
        Destination destination = session.createQueue(Consts.MQ_TEST_QUEUE_NAME);
        //创建一个生产者
        MessageProducer producer = session.createProducer(destination);


        TextMessage textMessage = session.createTextMessage("QueueProducer发送消息:" + msg);
        producer.send(textMessage);

        //关闭连接
        connection.close();
    }

}
